package com.pctx.rbac.controller;

import com.github.pagehelper.PageHelper;
import com.pctx.common.result.Result;
import com.pctx.rbac.pageHelper.PageInfo;
import com.pctx.rbac.pojo.Role;
import com.pctx.rbac.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by chason on 2018/2/24.16:26
 */

@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("list4company")
    public Result getByCompanyUuid(@RequestParam String companyUuid) throws Exception{
        List<Role> list = roleService.getByCompanyUuid(companyUuid);
        return Result.success(list);
    }

    @GetMapping("list4companyPage")
    public Result getByCompanyUuidPage(@RequestParam String companyUuid, @RequestParam(value = "page", defaultValue = "1") Integer page
            , @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception{
        PageHelper.startPage(page,pageSize);
        List<Role> list = roleService.getByCompanyUuid(companyUuid);
        return Result.success(new PageInfo<Role>(list));
    }

    @GetMapping("detail")
    public Result detail(@RequestParam String uuid) throws Exception{
        Role role = roleService.getByUuid(uuid);
        return Result.success(role);
    }

    @PostMapping("add")
    public Result add(Role role) throws Exception{
        boolean flag = roleService.add(role);
        if (flag)
            return Result.success(null);
        else
            return Result.error(null);
    }

    @PostMapping("edit")
    public Result edit(Role role) throws Exception{
        boolean flag = roleService.update(role);
        if (flag)
            return Result.success(null);
        else
            return Result.error(null);
    }

    @GetMapping("del")
    public Result del(@RequestParam String uuid) throws Exception{
        boolean flag = roleService.delete(uuid);
        if (flag)
            return Result.success(null);
        else
            return Result.error(null);
    }
}
